import 'package:file_transfer/layout/top_sliding_layout.dart';
import 'package:file_transfer/model/simple_model.dart';
import 'package:file_transfer/route/simple_route.dart';
import 'package:file_transfer/store/user_store.dart';
import 'package:file_transfer/utils/toast_util.dart';
import 'package:file_transfer/utils/url_util.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class LeftDrawerFragment extends StatelessWidget {
  const LeftDrawerFragment({super.key});

  Widget _top(){
    return Consumer<SimpleModel>(
      builder: (BuildContext context, value, Widget? child) {
        return Container(
          padding: const EdgeInsets.only(left: 20,top: 60),
          alignment: Alignment.centerLeft,
          width: double.infinity,
          height: 250,
          color: const Color(0xff007aff),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.start,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              InkWell(
                onTap: ()=>_isLogin(value,context),
                child: Container(
                  width: 100,height: 100,
                  child: Image.asset("assets/logo.png"),
                ),
              ),
              const SizedBox(height: 10),
              InkWell(
                onTap: ()=>_isLogin(value,context),
                child: Text(value.userInfo.name,style: const TextStyle(
                    fontSize: 30,
                    color: Colors.white,
                    fontWeight: FontWeight.normal
                ),),
              ),
            ],
          ),
        );
      },
    );
  }

  _isLogin(SimpleModel simpleModel,BuildContext context){
    if(!simpleModel.isLogin){
      SimpleRoute.instance().toLoginPage(context);
    }
  }


  _item(String title, {required VoidCallback onTap,Color bgColor = Colors.white,Color textColor = Colors.black}){
    return InkWell(
      onTap: onTap,
      child: Container(
        margin: const EdgeInsets.only(top: 5),
        padding: const EdgeInsets.all(8.0),
        color: bgColor,
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Text(title,style: TextStyle(
                color: textColor,
                fontSize: 18
            )),
            Icon(Icons.chevron_right_sharp,color: textColor,)
          ],
        ),
      ),
    );
  }

  _layout(BuildContext context){
    final simpleModel = context.read<SimpleModel>();
    delUserData().then((res){
      if(res){
        simpleModel.clearUserInfo();
        debugPrint("退出登录成功");
        ToastUtil.show("退出登录成功",backgroundColor: Colors.green);
        SimpleRoute.instance().toLoginPage(context);
      }
    });
  }


  @override
  Widget build(BuildContext context) {
    final simpleRoute = SimpleRoute.instance();
    return Container(
      color: Colors.white,
      width: MediaQuery.sizeOf(context).width * 3/5,
      child: Column(
        children: [
          _top(),
          Expanded(child: Column(
            children: [
              _item("弹出抽屉",onTap: ()=>TopSlidingLayout.of(context).togglePanel()),
              _item("接收文件",onTap: ()=>simpleRoute.toFilePage(context)),
              _item("个性设置",onTap: ()=>simpleRoute.toConfigPage(context)),
              _item("使用教程",onTap: ()=>UrlUtil.openHelp()),
              _item("关于软件",onTap: ()=>simpleRoute.toAboutPage(context)),
              _item("退出登录",bgColor: Colors.redAccent,textColor: Colors.white,onTap: ()=>_layout(context)),
            ],
          ))

        ],
      ),
    );
  }
}
